package com.dotstone.chipism.ui.security.adapter;

import android.support.annotation.Nullable;

import com.chad.library.adapter.base.BaseItemDraggableAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.util.MultiTypeDelegate;
import com.dotstone.chipism.R;
import com.dotstone.chipism.ui.security.MediaBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 条件选择-设备条件列表 适配器
 */
public class MediaAdapter extends BaseItemDraggableAdapter<MediaBean, BaseViewHolder> {
    boolean isCheckVisible = false;

    public MediaAdapter(@Nullable List<MediaBean> data) {
        super(data);
        init();
    }

    private void init() {
        setMultiTypeDelegate(new MultiTypeDelegate<MediaBean>() {
            @Override
            protected int getItemType(MediaBean bean) {
                return bean.itemType;
            }
        });

        getMultiTypeDelegate()
                .registerItemType(MediaBean.MEDIA_DAY, R.layout.item_day)
                .registerItemType(MediaBean.MEDIA_PIC, R.layout.item_pic)
                .registerItemType(MediaBean.MEDIA_VID, R.layout.item_vid);
    }

    @Override
    protected void convert(BaseViewHolder helper, MediaBean item) {
        if (item.itemType == MediaBean.MEDIA_DAY) {
            helper.setText(R.id.item_day, item.itemDay);
        }else if (item.itemType == MediaBean.MEDIA_PIC){

            helper.setText(R.id.item_date, item.itemDate)
                    .setVisible(R.id.item_check,isCheckVisible)
                    .setImageBitmap(R.id.item_pic,item.itemBitmap)
                    .setImageResource(R.id.item_check,item.itemCheck ? R.drawable.ic_set_check:R.drawable.ic_set_normal)
                    .addOnClickListener(R.id.item_check);
        }else if (item.itemType == MediaBean.MEDIA_VID){

            helper.setText(R.id.item_date, item.itemDate)
                    .setVisible(R.id.item_check,isCheckVisible)
                    .setImageBitmap(R.id.item_pic,item.itemBitmap)
                    .setImageResource(R.id.item_check,item.itemCheck ? R.drawable.ic_set_check:R.drawable.ic_set_normal)
                    .addOnClickListener(R.id.item_check);
        }
    }

    public void setCheckVisible(boolean isCheckVisible){
        this.isCheckVisible = isCheckVisible;
        notifyDataSetChanged();
    }

    public void setAllCheck(boolean isAllCheck){
        for (MediaBean mediaBean : getData()) {
            if (mediaBean.itemType == MediaBean.MEDIA_PIC || mediaBean.itemType == MediaBean.MEDIA_VID) {
                mediaBean.itemCheck = isAllCheck;
            }
        }
        notifyDataSetChanged();
    }

    public List<MediaBean> getCheckList(){
        List<MediaBean> list = new ArrayList<>();
        for (MediaBean mediaBean : getData()) {
            if ((mediaBean.itemType == MediaBean.MEDIA_PIC || mediaBean.itemType == MediaBean.MEDIA_VID)&&
                    mediaBean.itemCheck) {
                list.add(mediaBean);
            }
        }
        return list;
    }

    public void setCheck(int position){
        MediaBean mediaBean = getData().get(position);
        mediaBean.itemCheck = !mediaBean.itemCheck;
        notifyItemChanged(position);
    }

    public void setCheck(int position,boolean check){
        MediaBean mediaBean = getData().get(position);
        mediaBean.itemCheck = !mediaBean.itemCheck;
        notifyItemChanged(position);
    }
}